package com.py.credit.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Account;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.ResourceEvalInfoFieldsEnum;
import com.py.credit.service.EvalInfoService;
import com.py.credit.service.OrgService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;

@Api(tags = "activity", description = "前台评价流程")
@RestController
@RequestMapping(value = "/activity")
public class FrontActivityController {

	@Autowired
	private OrgService orgService;

	@Autowired
	private EvalInfoService evalInfoService;

	private final static String FRONT_F_TABLE_ID = "fCommitmentActivityTable";

	@RequestMapping(value = "/front/addFColumns.json")
	public RestServiceResult<String> addFShowColunms(HttpServletRequest request,
			@RequestParam(value = "columns", required = true) String[] columns) {

		Account account = (Account) request.getSession().getAttribute("ACCOUNT");

		if (account != null) {

			MapCache.set(account.getId() + "_" + FRONT_F_TABLE_ID, columns);

		} else

		{

			ExceptionLogger.error("FrontActivityController#addFColumns", "未获取到当前用户信息！");

		}

		return new RestServiceResult<String>();
	}

	@RequestMapping(value = "/front/getFTableList.json")
	public RestServiceResult<List<Map<String, Object>>> getFTableList(
			@RequestParam(value = "condition", defaultValue = "") String condition, String shzt, Page page,
			HttpServletRequest request) {

		Account account = (Account) request.getSession().getAttribute("ACCOUNT");

		String[] additionalColumns = { "id", "modelId", "sqr" };

		List<Map<String, Object>> tableList = null;

		if (account != null) {

			String checkType = account.getCheckType();

			if (StringUtils.isNotBlank(checkType)) {

				String[] checkTypes = checkType.split(",");

				if (checkTypes.length > 0) {

					String[] configColumns = (String[]) MapCache.get(account.getId() + "_" + FRONT_F_TABLE_ID);

					List<String> showColumns = (configColumns == null || configColumns.length == 0)
							? ResourceEvalInfoFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);

					tableList = orgService.getActivityByConditionForPage(showColumns, additionalColumns, condition,
							page, account.getOrgId(), checkType);
				}
			}

		} else {
			ExceptionLogger.error("FrontActivityController#getFTableList", "未获取到当前用户信息！");
		}

		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(),
				tableList == null ? new ArrayList<>(0) : tableList);

	}

	@RequestMapping(value = "/front/fExport")
	public void fExport(
			@ApiParam(required = true, name = "condition", value = "搜索条件") @RequestParam(value = "condition", defaultValue = "", required = true) String condition,
			@ApiParam(required = true, name = "columnEN", value = "导出列") @RequestParam(value = "columnEN", required = true) String columnEN,
			Page page, HttpServletResponse response, HttpServletRequest request) throws Exception {

		// 获取当前用户信息

		Account account = (Account) request.getSession().getAttribute("ACCOUNT");

		if (account != null) {

			List<String> columns = JSONArray.parseArray(columnEN, String.class);

			String checkType = account.getCheckType();
			page.setLimit(1000);

			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

			if (StringUtils.isNotBlank(checkType)) {

				String[] checkTypes = checkType.split(",");

				if (checkTypes.length > 0) {

					data = orgService.getActivityByConditionForPage(columns, null, condition, page, account.getOrgId(),
							checkType);

				}

				ExcelUtil.downloadExcel(response, data, "审核列表", "审核列表", columns,
						ResourceEvalInfoFieldsEnum.getOptionalColumns());
			}
		} else {
			ExceptionLogger.error("FrontActivityController#fExport", "未获取到当前用户信息！");
		}

	}

	@RequestMapping(value = "/front/getOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getOptionalColums() {
		return new RestServiceResult<List<OptionalColumn>>(ResourceEvalInfoFieldsEnum.getOptionalColumns());

	}

	@RequestMapping(value = "/front/getAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getAdvancedConditionColumns() {

		return new RestServiceResult<List<OptionalColumn>>(ResourceEvalInfoFieldsEnum.getAdvancedConditionColumns());

	}

	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/front/viewCheck.json")
	public RestServiceResult<List<Map>> viewCheck(String modelId, String sqr, String evalId,
			HttpServletResponse response, HttpServletRequest request) {
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		List<Map> list = evalInfoService.viewCheck(modelId, sqr, evalId);
		result.setData(list);
		return result;

	}

	@RequestMapping(value = "/front/preScore.json")
	public RestServiceResult<Object> preScore(String modelId, String sqr, String evalId, HttpServletResponse response,
			HttpServletRequest request) {
		evalInfoService.preScore(modelId, sqr, evalId);
		return new RestServiceResult<Object>();

	}

	@RequestMapping(value = "/front/updateModelIndexValue.json")
	public RestServiceResult<Object> updateModelIndexValue(String indexCatModelId, String sqr, String value,
			String evalId, HttpServletResponse response, HttpServletRequest request) {

		RestServiceResult<Object> result = new RestServiceResult<Object>();
		// 获取当前用户信息
		// evalInfoService.updateModelIndexValue(evalId, indexCatModelId, sqr,
		// value, String.valueOf(userInfo.getId()));
		return result;

	}

	@RequestMapping(value = "/front/updateModelIndexScore.json")
	public RestServiceResult<Object> updateModelIndexScore(String indexCatModelId, String modelId, String sqr,
			double value, String evalId, HttpServletResponse response, HttpServletRequest request) {

		evalInfoService.updateModelIndexScore(indexCatModelId, modelId, sqr, value, evalId);
		return new RestServiceResult<Object>();

	}

}
